<?php
namespace App\Service\AliyunService;

use App\Service\BaseService;
use App\Service\AppService\SystemCacheConfigService;
use Service\VendorClient\Aliyun\AliyunIpAddressQueryClient;
use Service\VendorClient\Amap\AmapApiClient;

/**
 * 查询ip地址服务中心
 * @author chenshubo
 */
class QueryIpAddressService extends BaseService
{  
    /**
     * 物流轨迹批量查询服务
     * @param array $param
     * @return mixed
     */
    public function QueryAddressByIp(string $ip)
    {
        $config = SystemCacheConfigService::instance()->getCacheInfoBySystemCode('aliyun_ip_query');
        if( empty($config) ) 
        {
            //如果没有配置阿里云ip地址解析功能，就使用高德功能，如果高德解析成功就继续使用，没有解析成功，则提示
            $config = [
                'key'       => 'acdcb4c7f369b09f8104a4783a8be9d2',
                'baseurl'   => 'https://restapi.amap.com/v3/ip'
            ];
            $result = (new AmapApiClient($config))->queryAddressByIp($ip);
        } else {
            $result = (new AliyunIpAddressQueryClient($config))->queryAddressByIp($ip);
        }
        return $result;
    }
}